﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_KyNangTinHocNgoaiNgu : System.Web.UI.Page
{
    SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            btnclose.Attributes.Add("onClick", "javascript:history.back(); return false;");
            BindKyNangNgoaiNgu();
            BindKyNangTinHoc();
            BindKyNangkhac();
        }
    }

    private void BindKyNangNgoaiNgu()
    {
        _dbDataContext _db = new _dbDataContext();
        string  madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        var list1 = _db.tblDanhMucs.Where(r => r.Loai == 4).ToList();
        var list2 = _db.tblKyNangNgoaiNgus.Where(r => r.MaDoanVien.Trim() == madoanvien).ToList();
        foreach (var item1 in list1)
        {
            if (list2.Where(r => r.NgoaiNguID == item1.ID).Count() == 0)
            {
                CatalogAccess.InsertKyNangNgoaiNgu(item1.ID, madoanvien);
            }
        }
        var val = from p in _db.tblKyNangNgoaiNgus.Where(r => r.MaDoanVien.Trim() == madoanvien)
                  select new { ID=p.ID,Name=p.tblDanhMuc.TenDanhMuc.Trim(),Nghe=p.Nghe,Noi=p.Noi,Doc=p.Doc,Viet=p.Viet,Chon=p.Chon};
        grid.DataSource = val.ToList();
        grid.DataBind();
    }
    private void BindKyNangTinHoc()
    {
        _dbDataContext _db = new _dbDataContext();
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        var list1 = _db.tblDanhMucs.Where(r => r.Loai == 5).ToList();
        var list2 = _db.tblKyNangTinHocs.Where(r => r.MaDoanVien.Trim() == madoanvien).ToList();
        foreach (var item1 in list1)
        {
            if (list2.Where(r => r.TinHocID == item1.ID).Count() == 0)
            {
                CatalogAccess.InsertKyNangTinHoc(item1.ID, madoanvien);
            }
        }
        var val = from p in _db.tblKyNangTinHocs.Where(r => r.MaDoanVien.Trim() == madoanvien)
                  select new { ID = p.ID, Name = p.tblDanhMuc.TenDanhMuc.Trim(), KyNang=p.KyNang, Chon = p.Chon };
        gridtinhoc.DataSource = val.ToList();
        gridtinhoc.DataBind();
    }
    private void BindKyNangkhac()
    {
        _dbDataContext _db = new _dbDataContext();
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        var list1 = _db.tblDanhMucs.Where(r => r.Loai == 1010).ToList();
        var list2 = _db.tblKyNangKhacs.Where(r => r.MaDoanVien.Trim() == madoanvien).ToList();
        foreach (var item1 in list1)
        {
            if (list2.Where(r => r.KyNangKhacID == item1.ID).Count() == 0)
            {
                CatalogAccess.InsertKyNangKhac(item1.ID, madoanvien);
            }
        }
        var val = from p in _db.tblKyNangKhacs.Where(r => r.MaDoanVien.Trim() == madoanvien)
                  select new { ID = p.ID, Name = p.tblDanhMuc.TenDanhMuc.Trim(), KyNang = p.KyNang, Chon = p.Chon };
        gridkhac.DataSource = val.ToList();
        gridkhac.DataBind();
    }
    protected void btnsavengoaingu_Click(object sender, EventArgs e)
    {
        int id;
        CheckBox chk;
        DropDownList nghe;
        DropDownList noi;
        DropDownList doc;
        DropDownList viet;
        foreach (GridViewRow dr in grid.Rows)
        {
            id = int.Parse(grid.DataKeys[dr.RowIndex].Value.ToString());
            chk = (CheckBox)grid.Rows[dr.RowIndex].FindControl("chkchon");
            if (chk.Checked == false)
            {
                CatalogAccess.UpdateKyNangNgoaiNgu(id, 0, 0, 0, 0, false);//o day do no false nen cac gia tri phia sau id khong can thiet
            }
            else
            {
                nghe = (DropDownList)grid.Rows[dr.RowIndex].FindControl("drlNghe");
                noi = (DropDownList)grid.Rows[dr.RowIndex].FindControl("drlNoi");
                doc = (DropDownList)grid.Rows[dr.RowIndex].FindControl("drlDoc");
                viet = (DropDownList)grid.Rows[dr.RowIndex].FindControl("drlViet");
                CatalogAccess.UpdateKyNangNgoaiNgu(id, int.Parse(nghe.SelectedValue.ToString()), int.Parse(noi.SelectedValue.ToString()), int.Parse(doc.SelectedValue.ToString()), int.Parse(viet.SelectedValue.ToString()), true);
            }
        }
        lblmessage.Text = "Đã lưu.";
    }
    protected void btnsavetinhoc_Click(object sender, EventArgs e)
    {
        int id;
        CheckBox chk;
        DropDownList kynang;
        foreach (GridViewRow dr in gridtinhoc.Rows)
        {
            id = int.Parse(gridtinhoc.DataKeys[dr.RowIndex].Value.ToString());
            chk = (CheckBox)gridtinhoc.Rows[dr.RowIndex].FindControl("chkchon");
            if (chk.Checked == false)
            {
                CatalogAccess.UpdateKyNangTinHoc(id, 0, false);
            }
            else
            {
                kynang = (DropDownList)gridtinhoc.Rows[dr.RowIndex].FindControl("drlTrinhDo");
                CatalogAccess.UpdateKyNangTinHoc(id, int.Parse(kynang.SelectedValue.ToString()),true);
            }
        }
        lblmessage1.Text = "Đã lưu.";
    }
    protected void btnsavekynangkhac_Click(object sender, EventArgs e)
    {
        int id;
        CheckBox chk;
        DropDownList kynang;
        foreach (GridViewRow dr in gridkhac.Rows)
        {
            id = int.Parse(gridkhac.DataKeys[dr.RowIndex].Value.ToString());
            chk = (CheckBox)gridkhac.Rows[dr.RowIndex].FindControl("chkchon");
            if (chk.Checked == false)
            {
                CatalogAccess.UpdateKyNangKhac(id, 0, false);
            }
            else
            {
                kynang = (DropDownList)gridkhac.Rows[dr.RowIndex].FindControl("drlKyNang");
                CatalogAccess.UpdateKyNangKhac(id, int.Parse(kynang.SelectedValue.ToString()), true);
            }
        }
        lblmessage2.Text = "Đã lưu.";
    }
}